import {useState, forwardRef, useImperativeHandle, useEffect} from 'react';
import PageModule from "@/components/PageModule.tsx";
import {Form, Row} from "antd";
import CycleForm from "@/components/DynamicSearchForm/CycleForm.tsx";
import {getDicOnCode} from "@/utils/dicAll.ts";

const VehicleInfo = forwardRef((props, ref) => {
    let {carAutomobileInfo, policyDisabled} = props;
    useImperativeHandle(ref, () => ({
        verifyTheForm: () => {
            return new Promise((resolve, reject) => {
                carInfoForm.validateFields().then((values) => {
                    resolve(values)
                }).catch((err) => {
                    reject({})
                })
            })
        }
    }))
    const [carInfoForm] = Form.useForm();
    let [formFields] = useState([
        {
            type: 'input',
            name: 'licenseno',
            label: '车牌号码',
            dependencies:['frameno','engineno'],
            rules: [
                ({getFieldValue}) => ({
                    validator(_, value) {
                        if (!value && !getFieldValue('frameno') && !getFieldValue('engineno')) {
                            return Promise.reject(new Error('请输入!'));
                        }
                        return Promise.resolve()
                    },
                }),
            ]
        },
        {
            type: 'input',
            name: 'frameno',
            label: '车架号',
            dependencies: ['licenseno', 'engineno'],
            rules: [
                ({getFieldValue}) => ({
                    validator(_, value) {
                        if (!value && !getFieldValue('licenseno') && !getFieldValue('engineno')) {
                            return Promise.reject(new Error('请输入!'));
                        }
                        return Promise.resolve()
                    },
                }),
            ]
        },
        {
            type: 'input',
            name: 'brandName',
            label: '厂牌型号',
        },
        {
            type: 'input',
            name: 'engineno',
            label: '发动机号',
            dependencies: ['licenseno', 'frameno'],
            rules: [
                ({getFieldValue}) => ({
                    validator(_, value) {
                        if (!value && !getFieldValue('licenseno') && !getFieldValue('frameno')) {
                            return Promise.reject(new Error('请输入!'));
                        }
                        return Promise.resolve()
                    },
                }),
            ]
        },
        {
            type: 'select',
            name: 'vechicleType',
            label: '机动车种类',
            placeholder: '请选择',
            options: getDicOnCode('PRODUCT.VECHICLETYPE"'),
        },
        {
            type: 'select',
            name: 'isnew',
            label: '新旧车',
            placeholder: '请选择',
            options: getDicOnCode('PRODUCT.CARRISKISNEW'),
        },
        {
            type: 'input',
            name: 'seatcount',
            label: '核定载客(位)',
        },
        {
            type: 'input',
            name: 'ishavedamage',
            label: '是否包含车损',
        },
        {
            type: 'select',
            name: 'useType',
            label: '使用性质',
            placeholder: '请选择',
            options: getDicOnCode('PRODUCT.CARUSETYPE'),
        },
        {
            type: 'input',
            name: 'toncount',
            label: '核定载质量(Kg)',
            placeholder: '请选择',
            },
        {
            type: 'input',
            name: 'displacement',
            label: '排量(L)',
        },
        {
            type: 'date',
            name: 'enrollDate',
            label: '登记日期',
        },
        {
            type: 'select',
            name: 'transferFlag',
            label: '是否过户',
            placeholder: '请选择',
            options: getDicOnCode("SYS.ISNOT"),
        }
    ])
    useEffect(() => {
        if (carAutomobileInfo){
            carInfoForm.setFieldsValue(carAutomobileInfo)
        }
    }, [carAutomobileInfo]);
    return (
        <div style={{marginTop: 20}}>
            <PageModule title="车辆信息" btns={<span>提示：车牌号、车架号、发动机号必填其一。</span>}>
                <Form
                    layout="horizontal"
                    autoComplete="off"
                    labelCol={{span: 8}}
                    wrapperCol={{span: 16}}
                    form={carInfoForm}
                    initialValues={{
                        vechicleType:undefined,
                        isnew:undefined,
                        useType:undefined,
                        transferFlag:undefined
                    }}
                    disabled={policyDisabled}
                >
                    <Row>
                        <CycleForm fields={formFields} />
                    </Row>
                </Form>
            </PageModule>
        </div>
    );
})

export default VehicleInfo;